Ccay.Module.define("Ccay.example.ui.widget.widgetFunction", function (page, $S) {
	page.ready = function(){
		var studentModal = Ccay.Modal.create({
			modalName: "student",
			modalKey: "id",
			fields: [
				 {field: "id", name:"User ID", renderLayout:true, renderType:"edit", enable: true},
				 {field: "name", name:"User Name", layout:"custom" },
				 {field: "sex", name: "User sex", type: "bool", format:{isIcon: true}, renderTmpl: "${student.sex}<a class='ccay-icon male' title='Male'></a>"},
				 {field: "age", name:"User Age", type:"integer"},
				 {field: "birthday", name:"User Birthday", type:"date"},
				 {field: "province", name: "User Province", type: "lookup", format: {code:"province", supportLang: true}}
			],
			rest:{
				createSingle:"services/ccay/demo/student/single",
				updateSingle:"services/ccay/demo/student/single",
				findSingle:"services/ccay/demo/student/single/${id}"
			}
		});
		
//		studentModal.name.rule = {required: true};
		
		var widgetOption = {
			modal: studentModal,
			title: $i18n("ccay.workflow.label.edit"),
			renderType: "edit",
			layout: "form-Flow",
			body: "<form class='init ccay-widget-body ccay-form'></form>",
			buttons: [
				{type: "input", text: "Update", css:"default submit"},
				{type: "input", text: "Get record", click: function () {
					var form = $S("#iStudent").widget();
					if (form.valid()) {
						alert(Ccay.DataHelper.toJsonStr(form.getValue()));
					}
				}},
//				{type: "input", text: "Reload", click: function () {//debugger;
//					var form = $('#iStudent').widget(),
//						id = form.findByName('id').getValue();
//					if (id) {
//						form.request(id);
//					} else {
//						Ccay.UI.topBox("fffffff");
//					}
//					
//				}},
				{type: "input", text:"Get Custom Setting", css:"getcs"}
			],
			events: {
				".submit click": function (e, form) {
					if(form.valid()) {
						var formValue = form.getValue(),
							modal = form.op.modal;
						!formValue.sex && (formValue.sex = 0);
						modal.setData(formValue);
						modal.saveSingle({});
					}
				},
				".getcs click": function (e, form) {
					alert(Ccay.DataHelper.toJsonStr(form.getCustomSetting()));
				}
			},
			ajax: studentModal.findSingle,
			param: {
				urlParam: {id: 100}
			}
		};
	    
		$S("#widgetFunctionDemo").ccayTab();
		
		//点击tab头后，才加载数据，只第一次生效
		$S("li[tabid='tab2']").one("click", function () {
			$S('#iStudent').widget(widgetOption);
		});
	};
	
	page.operateTest = function (value) {
		alert(Ccay.DataHelper.toJsonStr(value));
	};
});